using System;
using System.Collections.Generic;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;

namespace BPCSFileDaemon.FileFactory
{
    public class ExRateFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 21;

        public ExRateFileFactory(string path, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(path, DBConnectionString, DatabaseType)
        {
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawExRateDataManager CurDataManager = new RawExRateDataManager(this.CurrentSystemSession))
                    {
                        if (this.TryConnection())
                        {
                            // execute pre-action before inject data
                            CurDataManager.ExecutePreInjectAction();

                            // start to analyze
                            for (int x = 0; x < this.FileLines.Count; x++)
                            {
                                try
                                {
                                    string CurrentLine = this.FileLines[x].ToString();

                                    // break by offset
                                    if (CurrentLine.Length >= this.LineLength)  // 21 is the only length
                                    {
                                        string CurrencySource = CurrentLine.Substring(0, 3).Trim();    // size: 2
                                        string CurrencyTarget = CurrentLine.Substring(3, 3).Trim();    // size: 2
                                        string ExRateInStr = CurrentLine.Substring(6).Trim();

                                        decimal ExRate = Convert.ToDecimal(ExRateInStr) / 10000000;

                                        CurDataManager.AddData(CurrencySource, CurrencyTarget, ExRate);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    smooth = false;
                                    this.ErrMsg = "[ExRateFileFactory.ReadFile] : Exception : " + ex.Message;
                                }
                            }   // for

                            // inject now
                            if (CurDataManager.TotalInTheList > 0)
                            {
                                CurDataManager.ExecuteFinalAction();

                                if (!CurDataManager.ExecuteDataInject())
                                {
                                    smooth = false;
                                    this.ErrMsg = "[ExRateFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;
                                }
                            }
                        }
                        else
                        {
                            smooth = false;
                        }
                    }
                }
            }

            return smooth;
        }        
    }


}
